import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
#
# 读取CSV文件
file_path = 'High-speed rail.csv'
data = pd.read_csv(file_path)

# 提取需要的列
countries = data['Country']
operation = data['Operation']
under_construction = data['Under-construction']
planning = data['Planning']
#设置无衬线字体为微软雅黑
plt.rcParams['font.sans-serif'] = ['Microsoft Yahei']
# 各国运营里程对比柱状图
plt.figure(figsize=(4, 4))
plt.bar(countries, operation, color='steelblue')
plt.title('各国高铁运营里程对比')
plt.xlabel('国家')
plt.ylabel('运营里程 (千米)')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()


plt.figure(figsize=(4, 4))
plt.bar(countries, operation, label='运营', color='green')
plt.bar(countries, under_construction, bottom=operation, label='在建', color='orange')
plt.bar(countries, planning, bottom=operation + under_construction, label='计划', color='blue')
plt.title('各国高铁运营现状和发展')
plt.xlabel('国家')
plt.ylabel('里程 (千米)')
plt.xticks(rotation=45)
plt.legend()
plt.tight_layout()
plt.show()




plt.figure(figsize=(12,8))
plt.pie(operation, labels=countries, autopct='%1.2f%%', startangle=90,explode=[0.1,0,0,0,0,0], colors=['red', 'blue', 'green', 'orange', 'purple','pink'])
# 添加图例
plt.legend(countries, title="Country", loc="upper right")
#loc图例位置
plt.title('各国高铁运营里程占比')
plt.show()